package pmg;

import static org.junit.Assert.assertTrue;
import pmg.test.RobotTestBed;
import robocode.control.events.RoundEndedEvent;
import robocode.control.events.RoundStartedEvent;
import robocode.control.events.TurnEndedEvent;
import robocode.control.snapshot.IRobotSnapshot;
import java.awt.geom.Point2D;

/**
 * Tests WallEE's firing strategy which is to fire a bullet with power proportional to enemy
 * distance.
 * 
 * @author Paul Galiza
 */
public class TestWallEEFiringStrategy extends RobotTestBed {

  IRobotSnapshot[] robots;
  double enemyX = 0;
  double enemyY = 0;
  double WallEExLoc = 0;
  double WallEEyLoc = 0;
  double distance = 0;
  double enemyEnergy = 0;
  double enemyEnergyAfterFire = 0;
  boolean correctFiring = false;

  /**
   * Specifies that SittingDuck and WallEE are to be matched up in this test case.
   * 
   * @return The comma-delimited list of robots in this match.
   */
  @Override
  public String getRobotNames() {
    return "sample.SittingDuck,pmg.WallEE";
  }

  /**
   * This test runs for 1 rounds.
   * 
   * @return The number of rounds.
   */
  @Override
  public int getNumRounds() {
    return 1;
  }

  /**
   * Initializes all instance variables.
   * 
   * @param event round starting event
   */
  @Override
  public void onRoundStarted(RoundStartedEvent event) {
    robots = event.getStartSnapshot().getRobots();

    enemyX = robots[0].getX();
    enemyY = robots[0].getY();

    WallEExLoc = robots[1].getX();
    WallEEyLoc = robots[1].getY();

    enemyEnergy = robots[0].getEnergy();

    // calculates the distance between WallEE and enemy robot
    distance = Point2D.distance(enemyX, enemyY, WallEExLoc, WallEEyLoc);
  }

  /**
   * Updates necessary instance variables in every turn and then determines whether the firing
   * strategy is correct.
   * 
   * @param event turn ending event
   */
  @Override
  public void onTurnEnded(TurnEndedEvent event) {
    int turn = event.getTurnSnapshot().getTurn();
    robots = event.getTurnSnapshot().getRobots();
    enemyEnergyAfterFire = robots[0].getEnergy();

    // divides all the turns into manageable turns
    if (turn % 2 == 0) {
      enemyX = robots[0].getX();
      enemyY = robots[0].getY();
      WallEExLoc = robots[1].getX();
      WallEEyLoc = robots[1].getY();
    }

    // difference of the enemy energy before and after bullet hit
    double energyDelta = enemyEnergy - enemyEnergyAfterFire;

    // testing of the firing strategy
    if (distance <= 100 && (energyDelta == 16)) {
      correctFiring = true;
    }
    else if (distance > 100 && distance < 300 && (energyDelta == 10)) {
      correctFiring = true;
    }
    else if (energyDelta == 4) {
      correctFiring = true;
    }
  }

  /**
   * The actual test, which asserts that WallEE's firing strategy is working correctly.
   * 
   * @param event Details about the completed battle.
   */
  @Override
  public void onRoundEnded(RoundEndedEvent event) {
    assertTrue("Tests firing strategy", correctFiring);
  }
}
